package com.lsfb.signln.Statistics.WithoutDepNum;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.ajguan.library.EasyRefreshLayout;
import com.lsfb.signln.Login.LoginActivity;
import com.lsfb.signln.R;
import com.lsfb.signln.Statistics.HistoryRecord.HistoryRecordActivity;
import com.lsfb.signln.Utils.LittleUtils;
import com.lsfb.signln.Utils.LsfbUtils;
import com.lsfb.signln.Utils.MyProgressDialog;
import com.lsfb.signln.Utils.URLString;
import com.lsfb.signln.View.SignLoadview;
import com.lsfb.signln.View.SignRefreshHeadView;
import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.LsfbActivity;
import com.zgscwjm.lsfbbasetemplate.annotation.ContentView;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;
import com.zgscwjm.lsfbbasetemplate.utils.T;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@ContentView(R.layout.aty_without_depart_num)
public class WithoutChildDepNumActivity extends LsfbActivity implements PersonSignStatisticsAdapter.ItemClickListen {
    /**
     * 部门人员统计列表
     */
    @ViewInject(R.id.aty_without_depnum_rv)
    private RecyclerView rv_dep;
    private List<PersonSignStatisticsBean.ListBean> datas;
    private PersonSignStatisticsAdapter adapter;

    /**
     * 下拉刷新 上拉加载
     */
    @ViewInject(R.id.easylayout)
    private EasyRefreshLayout easylayout;

    private boolean IsRefresh = true;

    //人员类型
    private String usertype = "1";
    //时间类型
    private int timetype = 0;
    //开始时间
    private String start_time = "";
    //结束时间
    private String end_time = "";
    // 内勤/外勤
    private String moldType = "";
    //页数
    private int page = 0;
    //部门id
    private String depid;
    //部门名称
    private String depname;

    private MyProgressDialog dialog;

    @Override
    public void initView() {
        if (getIntent().getStringExtra("depid") != null) {
            depid = getIntent().getStringExtra("depid");
            depname = getIntent().getStringExtra("depname");
            timetype = getIntent().getIntExtra("timetype", -1);
            if (timetype == 3) {
                start_time = getIntent().getStringExtra("startTime");
                end_time = getIntent().getStringExtra("endTime");
            }
            moldType = getIntent().getStringExtra("moldType");
            int code = getIntent().getIntExtra("code", -1);
            switch (code) {
                case 0:
                    LittleUtils.setsimpletitlebar(this, depname + "内勤统计");
                    break;
                case 1:
                    LittleUtils.setsimpletitlebar(this, depname + "外勤统计");
                    break;
                case 2:
                    LittleUtils.setsimpletitlebar(this, depname + "外勤补录统计");
                    break;
            }
        } else {
            T.showShort(this, "数据获取异常");
            finish();
            return;
        }

        dialog = new MyProgressDialog(this, "加载中......");

        easylayout.setLoadMoreView(new SignLoadview(this));
        easylayout.setRefreshHeadView(new SignRefreshHeadView(this));
        easylayout.addEasyEvent(new EasyRefreshLayout.EasyEvent() {
            //加载更多
            @Override
            public void onLoadMore() {
                page++;
                IsRefresh = false;
                getData();
            }

            //刷新
            @Override
            public void onRefreshing() {
                page = 0;
                IsRefresh = true;
                getData();
            }
        });

    }

    /**
     * 获取内外勤部门签到数统计
     */
    private void getData() {
        HashMap hashMap = new HashMap();
        hashMap.put("type", usertype);
        hashMap.put("timetype", String.valueOf(timetype));
        hashMap.put("deps", depid);
        if (timetype == 3) {
            hashMap.put("starTime", start_time);
            hashMap.put("endTime", end_time);
        }
        hashMap.put("page", String.valueOf(page));
        hashMap.put("moldType", moldType);
        dialog.showdialog();
        new BaseInternet().getData(URLString.PERSONLISTINFO, hashMap, PersonSignStatisticsBean.class,
                new PersonSignStisticsEvent<PersonSignStatisticsBean>(), true);
    }

    @EventAnnotation
    public void eventResult(PersonSignStisticsEvent<PersonSignStatisticsBean> event) {
        dialog.hidedialog();
        easylayout.refreshComplete();
        easylayout.loadMoreComplete();
        switch (event.getCode()) {
            case 200:
                if (event.getData().getList() != null && !event.getData().getList().isEmpty()) {
                    if (IsRefresh) {
                        datas.clear();
                    }
                    datas.addAll(event.getData().getList());
                    adapter.setCount(event.getData().getCount());
                } else {
                    if (IsRefresh) {
                        datas.clear();
                    } else {
                        page--;
                        T.showShort(this, "没有更多数据");
                    }
                }
                adapter.notifyDataSetChanged();
                break;
            case 1006:
                T.showShort(this, "您的账号已在其它设备登录，请重登录");
                LittleUtils.exit(this);
                startActivity(new Intent(this, LoginActivity.class));
                break;
            default:
                T.showShort(this, event.getMsg());
                break;
        }
    }

    @Override
    public void initEvent() {

    }

    @Override
    public void initData() {
        datas = new ArrayList<>();
        adapter = new PersonSignStatisticsAdapter(this, datas, R.layout.item_single_crosswise, this);
        rv_dep.setLayoutManager(new LinearLayoutManager(this));
        rv_dep.addItemDecoration(LsfbUtils.getVERTICALLineDivider(this, R.color.white_color, 10));
        rv_dep.setAdapter(adapter);

        getData();
    }

    @Override
    public void Resume() {
        getData();
    }

    @Override
    public void onClick(PersonSignStatisticsBean.ListBean model) {
        startActivity(new Intent(this, HistoryRecordActivity.class).putExtra("person", model.getId())
                .putExtra("type", usertype).putExtra("timetype", timetype)
                .putExtra("startTime", start_time).putExtra("endTime", end_time)
                .putExtra("moldType", moldType));
    }
}
